package util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatUtil {

	/**
	 * 磁盘占用按照1000进制进行单位换算
	 * @param byteSize 文件字节大小
	 * @return
	 */
	public static String formatFileSize(long byteSize){
		DecimalFormat formater = new DecimalFormat("####.00");
        if(byteSize<1000){
            return byteSize+"B";
        }else if(byteSize<1000*1000){
            float kbsize = byteSize/1000f;
            return formater.format(kbsize)+"KB";
        }else if(byteSize<1000*1000*1000){
            float mbsize = byteSize/1000f/1000f;
            return formater.format(mbsize)+"MB";
        }else if(byteSize<1000*1000*1000*1000){
            float gbsize = byteSize/1000f/1000f/1000f;
            return formater.format(gbsize)+"GB";
        }else{
            return "size: error";
        }
	}

	/**
	 * 按照指定日期格式化字符串格式化日期毫秒数
	 * @param milliseconds 毫秒数
	 * @param formatStr 格式化字符串
	 * @return
	 */
	public static String formatDate(long milliseconds,String formatStr){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
		Date date = new Date();
		date.setTime(milliseconds);
		return simpleDateFormat.format(date);
	}
}
